package com.ruoyi.function.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.function.constant.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.function.mapper.ProductMapper;
import com.ruoyi.function.domain.Product;
import com.ruoyi.function.service.IProductService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-20
 */
@Service
public class ProductServiceImpl implements IProductService 
{
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public Product selectProductById(Long id)
    {
        return productMapper.selectProductById(id);
    }

    /**
     * 查询商品列表
     * 
     * @param product 商品
     * @return 商品
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        List<Product> products = productMapper.selectProductList(product);
        for (Product product1 : products) {

        }
        return products;
    }

    /**
     * 新增商品
     * 
     * @param product 商品
     * @return 结果
     */
    @Override
    public int insertProduct(Product product)
    {
        product.setCreateTime(DateUtils.getNowDate());
        return productMapper.insertProduct(product);
    }

    /**
     * 修改商品
     * 
     * @param product 商品
     * @return 结果
     */
    @Override
    public int updateProduct(Product product)
    {
        product.setUpdateTime(DateUtils.getNowDate());
        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(Long[] ids)
    {
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductById(Long id)
    {
        return productMapper.deleteProductById(id);
    }

    @Override
    public List<Product> listApp(Product product) {
        return productMapper.listApp(product);
    }

    @Override
    public Map<String,Product> calculateFee(BigDecimal distance) {
        Map<String,Product> map=new HashMap<>();
        Product product1 = new Product();
        product1.setServiceTypeId(BusinessConstants.dict.service_type_9);
        List<Product> products = productMapper.selectProductList(product1);
        for (Product product : products) {
            BigDecimal bigDecimal = calculateFee(product.getRemark(), distance);
            product.setPrice(bigDecimal);
            map.put(product.getName(), product);
        }
        return map;
    }


    public static BigDecimal calculateFee(String rule, BigDecimal distance) {
        // 解析规则中的基础费用、包含公里数和超出的单价
        int baseFee = 0;
        int includedKms = 0;
        int extraPerKm = 0;

        Pattern pattern = Pattern.compile("基础费用(\\d+).*含(\\d+)公里.*超过每公里加(\\d+)元");
        Matcher matcher = pattern.matcher(rule);
        if (matcher.find()) {
            baseFee = Integer.parseInt(matcher.group(1));
            includedKms = Integer.parseInt(matcher.group(2));
            extraPerKm = Integer.parseInt(matcher.group(3));
        } else {
            throw new IllegalArgumentException("费用规则格式不正确");
        }

        // 校验距离合法性
        if (distance.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("距离不能为负数");
        }

        // 转换为BigDecimal以便高精度计算
        BigDecimal baseFeeBD = new BigDecimal(baseFee);
        BigDecimal includedKmsBD = new BigDecimal(includedKms);
        BigDecimal extraPerKmBD = new BigDecimal(extraPerKm);

        // 判断是否超出基础距离
        if (distance.compareTo(includedKmsBD) <= 0) {
            return baseFeeBD;
        } else {
            // 计算超出部分的距离，并向上取整到整数（不足一公里按一公里计算）
            BigDecimal extraDistance = distance.subtract(includedKmsBD)
                    .setScale(0, RoundingMode.CEILING);  // 关键修改点
            BigDecimal extraCost = extraDistance.multiply(extraPerKmBD);
            return baseFeeBD.add(extraCost);
        }
    }

}
